package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"runtime"
	"strings"
	"wdhcr-jvm-cli/utils"
)

var installCmd = &cobra.Command{
	Use:   "install",
	Short: "Install remote warehouse jdk",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		option := args[0]

		install(option)
	},
}

func init() {
	rootCmd.AddCommand(installCmd)
}

func install(option string) {
	remoteJdkVersion := utils.GetRemoteJdkVersion(runtime.GOOS, runtime.GOARCH)
	var downloadUrl string

	for key, value := range remoteJdkVersion {
		if key == option {
			downloadUrl = value
			break
		}
	}
	if len(downloadUrl) <= 0 {
		fmt.Printf("invalid option:%s", option)
		return
	}
	fileName := strings.Replace(option, "jdk@", "", -1)
	download, err := utils.Download(downloadUrl, fileName+".dmg")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(download)
}
